package org.a019;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyContainer4 {
	volatile List list = new ArrayList<>();
	public void add(Object o){
		list.add(o);
	}
	public int size(){
		return list.size();
	}
	public static void main(String[] args) {
		MyContainer4 c= new MyContainer4();
		final Object o = new Object();
		new Thread(()->{
		synchronized (o) {
			System.out.println("t1启动");
			if (c.size() != 5) {
				try {
					o.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("t1结束");
			o.notify();
		}
		
		},"t1").start();
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(()->{
			synchronized (o) {
				for (int i = 0; i < 10; i++) {
					c.add(new Object());
					System.out.println("add " + i);
					if(i==5){
						try {
							o.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					o.notify();
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		},"t2").start();
		System.out.println("t2 结束");
	}
}
